package com.nokia.example3dgame;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class GameMIDlet extends MIDlet implements CommandListener {

private Display display;
private GameCanvasView gameCanvas;
private SplashScreenView splashScreen;
private Command exitCmd = new Command("Exit", Command.EXIT, 0);
private Command playCmd = new Command("Play", Command.SCREEN, 0);
private Command pauseCmd = new Command("Pause", Command.SCREEN, 0);
private Command playAgainCmd = new Command("Play Again", Command.SCREEN, 0);
private static String FRAMEWORK_INSTRUCTIONS = "framework/instructions.png";
private static String FRAMEWORK_PAUSED = "framework/paused.png";
private static String FRAMEWORK_WELLDONE = "framework/gameover_welldone.png";
private static String FRAMEWORK_BADLUCK = "framework/gameover_badluck.png";
private static String GAME_WEAPON = "game/weapon.png";
private static String GAME_FIRED = "game/fired.png";
private static int TIMEOUT = 4000;
private static String SPLASH_1 = "splashscreen/companyimage.png";
private static String SPLASH_2 = "splashscreen/splashimage.png";
private GameSoundManager soundManager;
private static String MIDI_MUSIC = "sounds/music.mid";
//private static String SOUND_SHOT = "sounds/shot.amr";
private static String SOUND_SHOT = "sounds/shot.mid";

public GameMIDlet() {
    this.display = Display.getDisplay(this);
    String[] midiFilenames = {MIDI_MUSIC};
    String[] gameSoundsFilenames = {SOUND_SHOT};
    soundManager = new GameSoundManager(midiFilenames, gameSoundsFilenames);

    Image[] images = new Image[2];
    images[0] = loadImage(SPLASH_1);
    images[1] = loadImage(SPLASH_2);

    Image frameworkPaused = loadImage(FRAMEWORK_PAUSED);
    Image frameworkInstructions = loadImage(FRAMEWORK_INSTRUCTIONS);
    Image frameworkWellDone = loadImage(FRAMEWORK_WELLDONE);
    Image frameworkBadLuck = loadImage(FRAMEWORK_BADLUCK);
    Image gameWeapon = loadImage(GAME_WEAPON);
    Image gameFired = loadImage(GAME_FIRED);
    this.gameCanvas = new GameCanvasView(this, soundManager, frameworkInstructions,
            frameworkPaused, frameworkWellDone, frameworkBadLuck, gameWeapon, gameFired);
    this.gameCanvas.addCommand(playCmd);
    this.gameCanvas.addCommand(exitCmd);
    this.gameCanvas.setCommandListener(this);
    this.splashScreen = new SplashScreenView(display, gameCanvas, images,
            TIMEOUT);
}

protected void startApp() throws MIDletStateChangeException {
    this.display.setCurrent(splashScreen);
    soundManager.playToneSequence();
}

protected void pauseApp() {
    // TODO Auto-generated method stub
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    this.gameCanvas.stopGame();
    this.gameCanvas = null;
    this.splashScreen = null;
    this.display = null;
}

public void commandAction(Command cmd, Displayable arg1) {
    if (cmd == playCmd) {
        this.gameCanvas.removeCommand(playCmd);
        this.gameCanvas.addCommand(pauseCmd);
        this.gameCanvas.playGame();
    } else if (cmd == pauseCmd) {
        this.gameCanvas.pauseGame();
        this.gameCanvas.removeCommand(pauseCmd);
        this.gameCanvas.addCommand(playCmd);
    } else if (cmd == playAgainCmd) {
        this.gameCanvas.removeCommand(playAgainCmd);
        this.gameCanvas.reset();
        this.gameCanvas.addCommand(playCmd);
    } else if (cmd == exitCmd) {
        notifyDestroyed();
    }
}

private Image loadImage(String image) {
    Image splashImage = null;
    try {
        splashImage = Image.createImage("/" + image);
    } catch (Exception e) {
        System.out.println("Couldn't load image " + image);
    }
    return splashImage;
}

public void gameOver() {
    this.gameCanvas.removeCommand(pauseCmd);
    this.gameCanvas.addCommand(playAgainCmd);
}
}
